//---------------------------------------------------------------------------
// CSignal Class
// Author : Woo-Hyun Kim
// Email : woorung@nhncorp.com
// Date : Apr. 7, 2008
//---------------------------------------------------------------------------
#ifndef CSignalH
#define CSignalH

#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h> 
#include <errno.h>

#include "CSignalHandler.h"
#include "CSignalMask.h"

using namespace std;
//---------------------------------------------------------------------------
class CSignal {
private :
	static CSignal *_instance; // current singleton instance
	static CSignalHandler *_signal_handlers[NSIG]; // registered signal handlers's array

	CSignal(void); // ensure singleton

	static void dispatcher(int signum, siginfo_t *si, void *sv);

public :
	static CSignal *instance(void); // get instance in singleton pattern

	// return the previous handler for signal chaining
	CSignalHandler *register_handler(int signum, CSignalHandler *sig_handler);
	CSignalHandler *get_handler(int signum);
	int remove_handler(int signum);

	void block(CSignalMask *sigmask);
	void unblock(CSignalMask *sigmask);
	void setmask(CSignalMask *sigmask);
	void setinit(CSignalMask *sigmask);

	int trigger(pid_t pid, int signum, const union sigval sv);
	int waitinfo(CSignalMask *&sigmask);
	int timedwaitinfo(CSignalMask *&sigmask, const struct timespec *timeout);
};
//---------------------------------------------------------------------------
#endif
